//
// Created by HhL on 2021/7/7.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../../include/c_utils.h"
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
struct person {
    int age;
    char name[10];
};
enum color {
    red, black, blue = 12, yellow, pink, green
};

//为类型取一个新的名字
typedef long long int32;
typedef struct Animal {
    int32 a;
    int32 b;
    int32 c;
    int32 d;
    int32 e;
    int32 f;
} animal_a;
struct student {
    struct person man;
    int score;
};

void test_struct();

void w2f();

void test_feof();

void test_feof1();

void wswap2f();

void wswap2f1();

void read_file();

int calc(char ch, int a, int b);

void read_file1();

void caucle();

void read_file08();

//采用C模式读二进制文件
void DataRead_CMode() {
  char*  filename = "/Users/hhl/Desktop/Architecture/jvm/c_study/file/HelloWorld.class";
    FILE* in_file = fopen(filename, "rb");
    if (!in_file) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }

    struct stat sb;
    if (stat(filename, &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }
    char* file_contents = malloc(sb.st_size);
    fread(file_contents, sb.st_size, 1, in_file);
    printf("read data: %s\n", file_contents);
    fclose(in_file);

    free(file_contents);
}
void DataRead_CMode1() {
    char*  filename = "/Users/hhl/Desktop/Architecture/jvm/c_study/file/HelloWorld.class";
    int fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("open\n");
        exit(EXIT_FAILURE);
    }

    struct stat sb;
    if (stat(filename, &sb) == -1) {
        perror("stat");
        exit(EXIT_FAILURE);
    }

    char* file_contents = malloc(sb.st_size);
    read(fd, file_contents, sb.st_size);

    printf("read data: %s\n", file_contents);
    close(fd);

    free(file_contents);
}

void test01() {
    //打开文件
//    FILE* pFile = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test08.txt", "r");
    FILE* pFile = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/Holloworld.class", "r");
    if (pFile == NULL) {
        printf("文件打开失败\n");
        return;
    }

    //统计有效行数
    int len = getFileLines(pFile);
    printf("文件的有效行数为：%d\n", len);
    char** pArray = malloc(sizeof(char*) * len);

    //读取文件中的数据并且放入到 pArray中
    readFileData(pFile, len, pArray);

    //读取数据
    showFileData(pArray, len);

    //释放堆区内容
    for (int i = 0; i < len; i++) {
        if (pArray[i] != NULL) {
            free(pArray[i]);
            pArray[i] = NULL;
        }
    }
    free(pArray);
    pArray = NULL;
    //关闭文件
    fclose(pFile);
}

int main() {
//    DataRead_CMode();
    DataRead_CMode1();
//    test01();
//    test_struct();

//    printf("score = %d\n", (&stu)->score);
//    w2f();
//    wswap2f();
//    wswap2f1();
//    read_file();
//    read_file1();
//    test_feof();
//    test_feof1();
//caucle();

//    getchar();
//    read_file08();
//    write_file("/Users/hhl/Desktop/Architecture/jvm/c_study/file/abc.txt");
//    read_file_now("/Users/hhl/Desktop/Architecture/jvm/c_study/file/abc.txt");
//    read_file2_cycle("/Users/hhl/Desktop/Architecture/jvm/c_study/file/abc.txt");
//    read_file3("/Users/hhl/Desktop/Architecture/jvm/c_study/file/abc.txt");
    return 0;
}

int calc(char ch, int a, int b) {
    switch (ch) {
        case '/':
            return a / b;

        case '+':
            return a + b;

        case '-':
            return a - b;

        case '*':
            return a * b;
        default:
            break;
    }
}

void caucle() {
    FILE* fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test08.txt", "w");
    if (fp == NULL) {
        perror("fopen error");
        return;
    }

    fputs("10/2=\n", fp);
    fputs("10*3=\n", fp);
    fputs("4-2=\n", fp);
    fputs("10+2=\n", fp);

    fclose(fp);
}

void read_file08() {
    char buf[4096] = {0};
    char result[4096] = {0};

    char sum_res[4096] = {0};

    int a, b, ret;
    char ch;

    FILE* fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test08.txt", "r");
    if (fp == NULL) {
        perror("fopen error");
        return;
    }

    while (1) {
        fgets(buf, 4096, fp);  //buf = "10/2=\n\0";
        if (feof(fp)) {
            break;
        }
        sscanf(buf, "%d%c%d=\n", &a, &ch, &b);    // a：10， ch：'/' b: 2

        sprintf(result, "%d%c%d=%d\n", a, ch, b, calc(ch, a, b));  // 10 / 2 = 5;

        strcat(sum_res, result);
    }
    fclose(fp);  // 将 只有表达式没有结果的文件关闭。

    fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test08.txt", "w");    // 清空 只有表达式没有结果的文件
    if (fp == NULL) {
        perror("fopen error");
        return;
    }
    fputs(sum_res, fp);    // 将 既有表达式又有结果的字符串写到文件中。

    fclose(fp);
}

void test_feof1() {
    FILE* fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt", "w");
    if (fp == NULL) {
        perror("fopen error");
    }
    char buf[4096] = {0};

    while (1) {
        fgets(buf, 4096, stdin);
        if (strcmp(buf, ":wq\n") == 0) {
            break;
        }
        fputs(buf, fp);
    }

    fclose(fp);
}

void test_feof() {
    FILE* fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt", "r");
    if (fp == NULL) {
        perror("fopen error");
        return;
    }
    while (1) {
        printf("没有到达文件结尾\n");
        fgetc(fp);                // 一次读一个字符，读到字符直接丢弃。
        if (feof(fp)) {
            break;
        }
    }
    fclose(fp);
}

void read_file1() {
    char ch = 0;

    FILE* fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt", "r");
    if (fp == NULL) {
        perror("fopen error");
        return;
    }

    while (1) {
        ch = fgetc(fp);

        if (feof(fp)) {
            break;
        }
        printf("%d\n", ch);
    }

    fclose(fp);
}

void read_file() {
    char ch = 0;
    FILE* fp = fopen("/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt", "r");
    if (fp == NULL) {
        perror("fopen error");
        return;
    }

    while (1) {//读出所有字节
        ch = fgetc(fp);
        if (ch == EOF) {
            break;
        }
        printf("%d\n", ch);
    }

    fclose(fp);
}

void wswap2f1() {
    char* filename = "/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt";
    FILE* fp = fopen(filename, "w");
    if (fp == NULL) {
        perror("fopen error");
    }

    int ret = fputc('A', fp);

    printf("ret = %d\n", ret);

    fclose(fp);
    printf("---------------finish\n");
}

void wswap2f() {
    char* filename = "/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt";
    int ret = 0;

    FILE* fp = fopen(filename, "w");
    if (fp == NULL) {
        perror("fopen error");
    }
    char ch = 'a'; //a -z
    //替换成26字母
    while (ch <= 'z') {
        ret = fputc(ch, fp);
        if (ret == -1) {
            perror("fputc eror");
        }
        ch++;
    }
}

void w2f() {
    char* buf = "12123112312312312";

    char* filename = "/Users/hhl/Desktop/Architecture/jvm/c_study/file/test04.txt";
    int ret = 0;

    FILE* fp = fopen(filename, "w");
    if (fp == NULL) {
        perror("fopen error");

    }
    int n = strlen(buf);
    for (size_t i = 0; i < n; i++) {
        ret = fputc(buf[i], fp);
        if (ret == -1) {
            perror("fputc eror");

        }
    }
    fclose(fp);


}

void test_struct() {
    /* 我的第一个 C 程序 */
    printf("Hello, World! \n");
    struct Animal stu;
    animal_a a1;

    a1.a = 10;
    a1.b = 20;
    printf("%d,%d", a1.a, a1.b);
//    int flg = yellow;
//
////    if (flg == 3)
//        printf("%d",flg);//前面那个加一？
//
//    struct student stu = { 18, 21, "BBB" };
//
//    printf("age = %d\n", (&stu)->man.age);
//    printf("name = %s\n", (&stu)->man.name);
}

